// 导入路由模块下得静态路由表
import { constantRoutes, asyncRoutes } from '@/router/index'

const state = () => {
  return {
    // 路由表数组： 初始值是静态路由表的值
    routes: constantRoutes
  }
}

const mutations = {
  // 设置路由表
  setRoutes: (state, showAsyncRoutes) => {
    // state.routes.push(...showAsyncRoutes)
    // 给routes数组追加筛选后的动态路由数组
    state.routes = [
      ...constantRoutes,
      ...showAsyncRoutes
    ]
  },
  //   删除路由表
  removeRoutes: (state) => {
    state.routes = []
  }

}

const actions = {
  // 筛选动态路由表
  filterAsyncRouteAction: ({ commit }, menus) => {
    // 基于动态路由表和menus数组筛选
    const showAsyncRoutes = asyncRoutes.filter(item => menus.includes(item.children[0].name))
    // console.log('showAsyncRoutes', showAsyncRoutes)
    // 提交mutation, 传入筛选结果作为参数
    commit('setRoutes', showAsyncRoutes)
    // 返回筛选结果（给路由守卫那边用）就是return出去
    return showAsyncRoutes
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
